package cn.phantom.pooltest;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

@Slf4j(topic = "c.TestForkJoin01")
public class TestForkJoin01 {
    public static void main(String[] args) {
        ForkJoinPool pool = new ForkJoinPool(4);
        Integer res = pool.invoke(new MyTask1(5));
        System.out.println(res);

    }

}

class MyTask1 extends RecursiveTask<Integer> {

    private int n;

    public MyTask1(int n) {
        this.n = n;
    }

    @Override
    protected Integer compute() {
        if (n == 1) {
            return 1;
        }
        MyTask1 t1 = new MyTask1(n - 1);
        t1.fork(); // 让一个线程去执行此任务
        int res = n + t1.join();
        return res;
    }
}
